//AUTHOR:  Aaron Kaufman
// Latest Edit:  12/2/2012
package gui;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.border.LineBorder;

import model.ConferencePaper;
import model.ProgramChair;
import model.SubProgramChair;

/*
 * This panel implements the following user stories, and associated business rules:
 * US1. As a Program Chair, I want to view a list of all submitted manuscripts and the acceptance status 
(yes/no/undecided) for each.
US2. As a Program Chair, I want to make an acceptance decision (yes or no) on a submitted manuscript.
US5. As a Program Chair, I want to see which papers are assigned to which Subprogram chairs.
US6. As a Program Chair I want to designate a Subprogram Chair for a manuscript.

 * Edited By Darrion Harris
 * 
 * 
 */
public class ProgramChairViewPanel extends JPanel{
    
    
    
    private ProgramChair my_chair;
    private JPanel my_manuscript_panel;
    private JPanel my_spc_panel;
    private JPanel card_panel;
    public static final String MANUSCRIPT_PANEL = "manuscript";
    public static final String SPC_PANEL = "spc_assigner";
    
    
    public ProgramChairViewPanel(ProgramChair the_chair)
    {
        my_chair = the_chair;
        rebuild();
        }
    
    // Edited By: Darrion Harris
    //Builds out the panel, and is used for refreshing after changes are made.
    public void rebuild()
    {
        this.removeAll();
        this.setBackground(BaseViewFrame.BACKGROUND_COLOR);
        JPanel inner_manuscript_box_panel = new JPanel();
        inner_manuscript_box_panel.setLayout(new BoxLayout(inner_manuscript_box_panel, BoxLayout.Y_AXIS));

        CardLayout card = new CardLayout();
        card_panel = new JPanel();
        card_panel.setLayout(card);
        
        GridLayout g = new GridLayout(0,5);
        g.setHgap(10);
        g.setVgap(10);
        my_manuscript_panel = new JPanel(g);
        my_spc_panel = new SPCAssigner(my_chair, this, card_panel);
        
        this.add(card_panel);
        
        my_manuscript_panel.add(getBorderedLabel("Paper Title"));
        my_manuscript_panel.add(getBorderedLabel("Author"));
        my_manuscript_panel.add(getBorderedLabel("Subprogram Chair"));
        my_manuscript_panel.add(getBorderedLabel("Recommendation"));
        my_manuscript_panel.add(getBorderedLabel("Acceptance Status"));
        my_manuscript_panel.setOpaque(false);
        
        addSpacers(5);
        
        for (ConferencePaper paper : my_chair.getPapers())
        {
            
            my_manuscript_panel.add(new TextLabel(paper.getTitle()));
            my_manuscript_panel.add(new TextLabel(paper.getAuthorName()));
            my_manuscript_panel.add(getSubProgramChairComboBox(paper));
            File file = new File(paper.getRecommendation());
            my_manuscript_panel.add(new TextLabel(file.getName()));
            my_manuscript_panel.add(getDecisionStatusComboBox(paper));
           
        }
        for (Component c : my_manuscript_panel.getComponents()) 
        {
        	c.setPreferredSize(new Dimension((BaseViewFrame.FRAME_WIDTH - 50)/5, 30));
        }
        JPanel bot_panel = new JPanel();
        bot_panel.add(getSPCCreator());
        
        
        inner_manuscript_box_panel.add(my_manuscript_panel);
        inner_manuscript_box_panel.add(new JPanel());
        inner_manuscript_box_panel.add(bot_panel);
        

        card_panel.add((inner_manuscript_box_panel), MANUSCRIPT_PANEL);
        card_panel.add((my_spc_panel), SPC_PANEL);
        
        card.show(card_panel, MANUSCRIPT_PANEL);
    }
    
    /*
     * Wrapping JLabel for displaying potentially long titles and names.
     */
    private class TextLabel extends JTextArea
    {
        public TextLabel(String the_string)
        {
            super(the_string);
            super.setBackground(null);
            super.setEditable(false);
            super.setLineWrap(true);
            super.setWrapStyleWord(true);
        }
    }
    
    //Creates a new SPCAssigner panel for the Program Chair.
    private JButton getSPCCreator()
    {
        JButton button = new JButton("Designate New Subprogram Chair");
        final CardLayout card = (CardLayout) card_panel.getLayout();
        final Container c = card_panel;
        button.addActionListener(new AbstractAction(){
            @Override
            public void actionPerformed(ActionEvent the_arg0)
            {
                card.show(c, SPC_PANEL);
                
            }
        });
        return button;
        
    }
    

    /*
     * Creates the subprogram chair dropdown menu for each paper.
     * Will not allow more than four papers to be assigned.
     */
    private JComboBox getSubProgramChairComboBox(final ConferencePaper the_paper)
    {
        final JComboBox combo_box = new JComboBox();
        for ( String sub_chair : my_chair.getSubprogramChairs().keySet())
        {
            if (!the_paper.getAuthorName().equals(sub_chair))
            {
                combo_box.addItem(sub_chair);
            }
        }
        combo_box.setSelectedItem(getSubChairAssigned(the_paper));
        combo_box.addActionListener(new AbstractAction(){

            @Override
            public void actionPerformed(ActionEvent the_arg0)
            {        
                
            
                
                String spc_string = (String) combo_box.getSelectedItem();
                SubProgramChair spc = my_chair.getSubprogramChairs().get(spc_string);
                
                if (spc.getMyPapers().size() > 3 && !spc.getMyPapers().contains(the_paper))
                {
                    JOptionPane.showMessageDialog(null, "Subprogram Chairs cannot be assigned more than four papers.");
                    combo_box.setSelectedItem(getSubChairAssigned(the_paper));
                    return;
                }
                reassignPaper(the_paper, spc);
            }
            
        });
        
        return combo_box;
        
    }
    
    private void addSpacers(int the_num)
    {
        for (int i = 0; i < the_num; i++)
        {
            my_manuscript_panel.add(new JLabel());
        }
    }
    //Creates the dropdown box for a given paper.
    private JComboBox getDecisionStatusComboBox(final ConferencePaper the_paper)
    {
        final JComboBox combo_box = new JComboBox();
        combo_box.addItem(ConferencePaper.UNDECIDED);
        combo_box.addItem(ConferencePaper.REJECTED);
        combo_box.addItem(ConferencePaper.ACCEPTED);
        combo_box.setSelectedItem(the_paper.getAcceptanceStatus());
        combo_box.addActionListener(new AbstractAction(){

           
            @Override
            public void actionPerformed(ActionEvent the_arg0)
            {
                the_paper.setAcceptance((String)combo_box.getSelectedItem());
            }
            
        });
       
        return combo_box;
    }
    
    /*
     * Gets label used for titles.
     */
    private JLabel getBorderedLabel(String the_string)
    {
        
        JLabel label = new JLabel(the_string);
        label.setBorder(new LineBorder(Color.BLACK));
        return label;
    }
    

    
    /*
     * Assigns the paper to a subprogramchair, and unassigns the paper from all other subprogramchairs.
     */
    private void reassignPaper(ConferencePaper the_paper, SubProgramChair the_chair)
    {
        for (SubProgramChair spc: my_chair.getSubprogramChairs().values())
        {
            
            spc.getManuscripts().remove(the_paper);
            
        }
        the_chair.addManuscipt(the_paper);
    }
    
    
    //Gets the subprogram chair assigned to the paper.
    private String getSubChairAssigned(ConferencePaper the_paper)
    {
        for (String key: my_chair.getSubprogramChairs().keySet())
        {
            SubProgramChair sub = my_chair.getSubprogramChairs().get(key);
            if (sub.getManuscripts().contains(the_paper))
            {
                return key;
            }
        }
        return null;
    }

}
